import pathToRegexp from 'path-to-regexp/index';
import {getInspep,postInspep,getIns,goListIN,postList,getInspepTow} from 'services/app';
import { routerRedux } from 'dva/router';

export default {
    namespace: 'inspep',
    state: {
        pages:1,
        record:{},
        numFrom:[{}],
        isnameADD:[],
        fenshuNUm:[],
        Checkboxit:[],
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
                if(location.pathname == '/Inspectionproject'){
                // if(location.href.split("/")[location.href.split("/").length - 1 ] == 'Inspectionproject'){
                    dispatch({type:'save',payload:{POSTTOWView:false,POSTONEView:false}});
                }
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //查看登录状态
        *getInspep ({ payload }, { call, put }){
            const {data,total} = yield call(getInspep, payload);
            if(location.pathname == '/Inspectionproject_list_option'){
            // if(location.href.split("/")[location.href.split("/").length - 1 ] == 'Inspectionproject_list_option'){
                yield put(routerRedux.push('/Inspectionproject'));
            }
            yield put({ type: 'save', payload: {getInspepData:data,total:total,pages:payload.pageNumber,Pagination:true}});
        },
        //新增查询
        *newpost ({ payload }, { call, put }){
            const {data} = yield call(getIns, {
                name: "",
                pageNumber :0,
                pageSize :0,
            });
            yield put({ type: 'save', payload: {newpostdata:data}});
        },
        //详情添加(废弃)
        *postList ({ payload }, { call, put }){
            const data = yield call(postList, payload);
            if(data.code == 0){
                //修改详情==>
                const {data} = yield call(goListIN, {id:localStorage.getItem('id')});
                yield put({ type: 'save', payload: {goListData:data,visible:false}});
            }
        },
        //详情(废弃)
        *goList ({ payload }, { call, put }){
            const {data} = yield call(goListIN, payload);
            localStorage.setItem('id',payload.id);
            yield put(routerRedux.push('/Inspectionproject_list'));
            yield put({ type: 'save', payload: {goListData:data}});
        },
        //详情(新)
        *goListGAI ({ payload }, { call, put }){
            const {data} = yield call(goListIN, payload);
            // localStorage.setItem('id',payload.id);
            yield put(routerRedux.push('/Inspectionproject_list_option'));
            yield put({type:'newpost'});
            let isnameADD= [],fenshuNUm = [],Checkboxit = [],numFrom =[];
            data.details.map((item,index)=>{
                isnameADD[index] = item.itemdetailname;
                fenshuNUm[index] = item.itemdetailnum;
                Checkboxit[index] = item.istrue == 1?true:false;
                numFrom[index] = {};
            });
            if(data.item_type == '1'){
                yield put({ type: 'save', payload: {
                    record:data,
                    POSTTOWView:true,
                    item_nameADD:data.item_name,
                    is_cal_score:data.is_cal_score,
                    category_id:data.category_id,
                    item_explain:data.item_explain,
                    isnameADD:isnameADD,
                    fenshuNUm:fenshuNUm,
                    Checkboxit:Checkboxit,
                    numFrom:numFrom,
                }});
            }else {
                yield put({ type: 'save', payload: {
                    record:data,
                    POSTONEView:true,
                    item_nameADD:data.item_name,
                    is_cal_score:data.is_cal_score,
                    wrong_num:data.wrong_num,
                    maxnum:data.maxnum,
                    correct_num:data.correct_num,
                    minnum:data.minnum,
                    category_id:data.category_id,
                    item_explain:data.item_explain,
                }});
            }
        },
        //添加项目
        *postInspep ({ payload }, { call, put }){
            const data = yield call(postInspep, payload);
            if (data.code == 0){
                yield put(routerRedux.push('/Inspectionproject'));
                yield put({ type: 'save', payload: {visible:false,pages:0}});
            }
        },
        //数字检查
        *goPOSTONEView ({ payload }, { call, put }){
            yield put(routerRedux.push('/Inspectionproject_list_option'));
            yield put({ type: 'save', payload: {
                record:{},
                POSTONEView:true,
                item_nameADD:'',
                is_cal_score:'',
                wrong_num:"",
                maxnum:'',
                correct_num:'',
                minnum:"",
                category_id:"",
                item_explain:"",
            }});
        },
        //选择检查
        *goPOSTTOWView ({ payload }, { call, put }){
            yield put(routerRedux.push('/Inspectionproject_list_option'));
            yield put({ type: 'save', payload: {
                record:{},
                POSTTOWView:true,
                item_nameADD:"",
                is_cal_score:"",
                category_id:"",
                item_explain:"",
                isnameADD:[],
                fenshuNUm:[],
                Checkboxit:[],
            }});
        },
        //跳转查询
        *getInspepTow ({ payload }, { call, put }){
            const {data,total} = yield call(getInspepTow, payload);
            localStorage.removeItem('hrefid');
            yield put({ type: 'save', payload: {getInspepData:data,total:total,Pagination:false}});
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
